﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;

using SBPweb.Modules.Modularity;

using SBPweb.MediaCenter.Common.Services;

namespace SBPweb.MediaCenter.Modules.Organizer.WPF.Controls
{
    /// <summary>
    /// Interaction logic for FolderFilesView.xaml
    /// </summary>
    public partial class FolderFilesView : ListView
    {

        #region Private fields

        private DirectoryInfo folder;
        private IModule parentModule;

        #endregion

        #region Constructor logic

        public FolderFilesView()
        {
            InitializeComponent();
        }

        public FolderFilesView(IModule parentModule, DirectoryInfo folder)
        {
            InitializeComponent();
            this.ParentModule = parentModule;
            this.Folder = folder;
        }

        #endregion

        #region Property accessors

        public DirectoryInfo Folder
        {
            get
            {
                return folder;
            }
            set
            {
                bool b = folder != value;
                folder = value;
                if (b)
                {
                    PopulateItems();
                }
            }
        }

        public IModule ParentModule
        {
            get
            {
                return parentModule;
            }
            set
            {
                parentModule = value;
            }
        }

        public IOrganizerService Organizer
        {
            get
            {
                return parentModule.Services.Get<IOrganizerService>();
            }
        }

        public IUriListOpenerService ListOpener
        {
            get
            {
                return parentModule.Services.Get<IUriListOpenerService>();
            }
        }

        #endregion

        #region Methods

        public void PopulateItems()
        {
            IList<string> extensions = Organizer.GetSupportedExtensions();
            this.Items.Clear();
            foreach (FileInfo fI in folder.GetFiles())
            {
                if (extensions.Contains(fI.Extension.Replace(".", string.Empty)))
                {
                    FolderFileItem fFI = new FolderFileItem(fI);
                    this.Items.Add(fFI);
                }
            }
        }

        #endregion

    }

}